/**
 * 
 */
package org.jenkon.iterator;

/**
 * @author Jens Konopka
 * 
 * @param <E>
 *            the type of values
 */
public class ArrayIterator<E> implements OrderedIterator<E> {

	private final E[] values;
	private E current;
	private int position = -1;

	/**
	 * @param values
	 */
	public ArrayIterator(final E[] values) {
		this.values = values;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Iterator#hasNext()
	 */
	public boolean hasNext() {
		return position < values.length - 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Iterator#next()
	 */
	public E next() {
		current = values[++position];
		return current;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Iterator#remove()
	 */
	public void remove() {
		throw new UnsupportedOperationException("remove ist not supported on "
				+ this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.iterator.OrderedIterator#hasPrevious()
	 */
	public boolean hasPrevious() {
		return position > 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.iterator.OrderedIterator#previous()
	 */
	public E previous() {
		current = values[--position];
		return current;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.iterator.OrderedIterator#current()
	 */
	public E current() {
		return current;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.iterator.OrderedIterator#last()
	 */
	public E last() {
		position = values.length;
		current = values[--position];
		return current;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.iterator.OrderedIterator#first()
	 */
	public E first() {
		position = -1;
		return next();
	}

}
